package com.springjdbc.controller;

import com.springjdbc.netity.User;
import com.springjdbc.service.UserService;
import com.springjdbc.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加
     *
     * @return
     */
    @PostMapping("/user/add")
    public void addUser(User user) {
        this.userService.insertUser(user);
    }


    /**
     * 查询
     *
     * @return
     */
    @GetMapping("/user/find")
    public List<User> selectUserList() {
        return this.userService.selectUserList(null);
    }


    /**
     * 批量添加
     *
     * @return
     */
    @GetMapping("/user/batchAdd")
    public Object add() {
        SnowFlake snowFlake = new SnowFlake(5, 6);
        for (int i = 0; i < 50; i++) {
            long id = snowFlake.nextId(false);
            User user = new User();
            user.setId(id);
            user.setUsername("batchName" + i);
            user.setPassword("12345" + i);
            userService.insertUser(user);
        }
        return "ok";
    }
}
